export class BrowserStorage {
  //#region sessionStorage
  /**
   * Saves the data on the local client side sessionStorage.
   * @param key The key string is the identifier of the data.
   * @param data The data which will be saved.
   */
  static saveSessionData(key: string, data: any): void {
    sessionStorage.setItem(key, JSON.stringify(data));
  }

  /**
   * Checks if the data on the local client side sessionStorage exists.
   * @param key The key string is the identifier of the data.
   */
  static sessionDataExists(key: string): boolean {
    return sessionStorage.getItem(key) != null;
  }

  /**
   * Loads the specific data from the local client side sessionStorage.
   * @param key The key string is the identifier of the data.
   * @param defaultValue Optional, will returned if there is no value for provided key found.
   * @return The data object.
   */
  static loadSessionData(key: string, defaultValue?: any): any {
    const jsonValue = sessionStorage.getItem(key);

    if (jsonValue) {
      return JSON.parse(jsonValue);
    }

    return defaultValue;
  }

  /**
   * Removes the specific data from the local client side sessionStorage.
   * @param key The key string is the identifier of the data.
   * @return The data object.
   */
  static removeSessionData(key: string): void {
    sessionStorage.removeItem(key);
  }
  //#endregion

  //#region localStorage
  /**
   * Saves the data on the local client side localStorage.
   * @param key The key string is the identifier of the data.
   * @param data The data which will be saved.
   */
  static saveLocalData(key: string, data: any): void {
    localStorage.setItem(key, JSON.stringify(data));
  }

  /**
   * Checks if the data on the local client side localStorage exists.
   * @param key The key string is the identifier of the data.
   */
  static localDataExists(key: string): boolean {
    return localStorage.getItem(key) != null;
  }

  /**
   * Loads the specific data from the local client side localStorage.
   * @param key The key string is the identifier of the data.
   * @param defaultValue Optional, will returned if there is no value for provided key found.
   * @return The data object.
   */
  static loadLocalData(key: string, defaultValue?: any): any {
    const jsonValue = localStorage.getItem(key);

    if (jsonValue) {
      return JSON.parse(jsonValue);
    }

    return defaultValue;
  }

  /**
   * Removes the specific data from the local client side localStorage.
   * @param key The key string is the identifier of the data.
   * @return The data object.
   */
  static removeLocalData(key: string): void {
    localStorage.removeItem(key);
  }
  //#endregion
}
